/*
 ===============================================================================
 Name        : main.cpp
 Author      : $(user)
 Version     : 1.0.0
 Date		 : $(date)
 Copyright   :
 License	 : CC BY-SA 3.0
 Description :
 ===============================================================================
 History
 ---------+---------+--------------------------------------------+-------------
 DATE     |	VERSION |	DESCRIPTIONS							 |	By
 ---------+---------+--------------------------------------------+-------------

 ===============================================================================
 */
#include "driver_config.h"
#ifdef __USE_CMSIS
#include "LPC11xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP;

// TODO: insert other include files here
#include "gpio.h"
#include "timer16.h"

// LPCXpresso processor card LED
#define LED_PORT 0		// Port for led
#define LED_BIT 7		// Bit on port for led
#define LED_ON 1		// Level to set port to turn on led
#define LED_OFF 0		// Level to set port to turn off led

extern volatile uint32_t timer16_0_counter;

int main(void) {

	GPIOInit();

	// Initialise Timer16_0
	init_timer16(0, 1000);	// with interval 1000us (1ms)

	// Initialise counter that counts Timer16_0 ticks
	timer16_0_counter = 0;

	//Enable Timer16_0
	enable_timer16(0);

	// Set port for LED to output
	GPIOSetDir(LED_PORT, LED_BIT, 1);

	// Enter an infinite loop
	while (1) {

		// LED is on for 1st half-second
		if ( (timer16_0_counter > 0) && (timer16_0_counter <= 1000) )
		{
		  GPIOSetValue( LED_PORT, LED_BIT, LED_OFF );
		}
		// LED is off for 2nd half-second
		if ( (timer16_0_counter > 1000) && (timer16_0_counter <= 2000) )
		{
			GPIOSetValue( LED_PORT, LED_BIT, LED_ON );
		}
		// Reset counter
		else if ( timer16_0_counter > 2000 )
		{
		  timer16_0_counter = 0;
		}
	}
	return 0;
}

//
// FreeRTOS Hook Functions
//
void vApplicationIdleHook() {
	// TODO: FreeRTOS Idle Hook
}

void vApplicationTickHook() {
	// TODO: FreeRTOS Tick Hook
}

void vApplicationMallocFailedHook() {
	// TODO: Faile malloc Hook
}
